package java67_adv10;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class ModifyXML {
	//
	public static final String XML_NAME = "src/java67_adv10/courses2.xml";

	// xml的增删改
	public static void main(String[] args) {
		// 构建输入对象
		Scanner in = new Scanner(System.in);
//		System.out.println("要修改哪个?");
//		String name = in.next();
//		// del(name);
//		System.out.println("输入新课时和描述");
//		update(name, in.nextInt(), in.next());
		// add(4, 4.5F, "前端开发", 30, "html5,css3,javascript");
		createAndAdd(1, 2.5F, "java基础", 10, "java");
	}

	// 从无到有,创建一个xml文档
	public static void createAndAdd(int index, float diff, String name, int time, String desc) {
		SAXReader reader = new SAXReader();
		Document doc = null;
		try {
			doc = reader.read(new File(XML_NAME));
		} catch (DocumentException e) {
			// System.out.println("文档出问题了");
			e.printStackTrace();
			// return;

		}
		if (doc == null) {
			// 创建一个Document对象,创建一个根节点
			doc = DocumentHelper.createDocument();
			doc.addElement("courses");// 加入了根节点
		}
		Element root = doc.getRootElement();
		Element p1 = root.addElement("product");
		p1.addAttribute("index", String.valueOf(index));
		p1.addAttribute("difficulty", String.valueOf(diff));
		Element p2 = p1.addElement("name");
		p2.setText(name);
		Element p3 = p1.addElement("coursetime");
		p3.setText(String.valueOf(time));
		Element p4 = p1.addElement("desc");
		p4.setText(desc);

		// 写完,写回去xml
		try {
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("utf-8");

			XMLWriter xm = new XMLWriter(new FileOutputStream(XML_NAME), format);

			xm.write(doc);
			xm.close();
			System.out.println("add成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 新增一个course节点
	public static void add(int index, float diff, String name, int time, String desc) {
		SAXReader reader = new SAXReader();
		Document doc = null;
		try {
			doc = reader.read(new File(XML_NAME));
		} catch (DocumentException e) {
			System.out.println("文档出问题了");
			e.printStackTrace();
			return;
		}
		Element root = doc.getRootElement();
		Element p1 = root.addElement("product");
		p1.addAttribute("index", String.valueOf(index));
		p1.addAttribute("difficulty", String.valueOf(diff));
		Element p2 = p1.addElement("name");
		p2.setText(name);
		Element p3 = p1.addElement("coursetime");
		p3.setText(String.valueOf(time));
		Element p4 = p1.addElement("desc");
		p4.setText(desc);

		// 写完,写回去xml
		try {
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("utf-8");

			XMLWriter xm = new XMLWriter(new FileOutputStream(XML_NAME), format);

			xm.write(doc);
			xm.close();
			System.out.println("add成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 修改:根据course的name改coursetime 和 desc

	public static void update(String name, int time, String desc) {
		SAXReader reader = new SAXReader();
		Document doc = null;
		try {
			doc = reader.read(new File(XML_NAME));
		} catch (DocumentException e) {
			System.out.println("文档出问题了");
			e.printStackTrace();
			return;
		}
		Element ename = (Element) doc.selectSingleNode("/courses/course[name='" + name + "']");
		if (ename == null) {
			System.out.println("没有这个" + name + "课程");
			return;
		}
		// 找到的course节点,找他的子节点coursetime和desc
		ename.selectSingleNode("coursetime").setText(String.valueOf(time));
		ename.selectSingleNode("desc").setText(desc);
		// 保存文件
		try {
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("utf-8");

			XMLWriter xm = new XMLWriter(new FileOutputStream(XML_NAME), format);

			xm.write(doc);
			xm.close();
			System.out.println("修改成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 根据name删除课程
	public static void del(String name) {
		SAXReader reader = new SAXReader();
		Document doc = null;
		try {
			doc = reader.read(new File(XML_NAME));
		} catch (DocumentException e) {
			System.out.println("文档出问题了");
			e.printStackTrace();
			return;
		}
		System.out.println(String.format("/courses/course[name='%s']", name));
		Element ename = (Element) doc.selectSingleNode(String.format("/courses/course[name='%s']", name));
		if (ename == null) {
			System.out.println("没有这个" + name + "课程");
			return;
		}
		// 获得ename的上级,然后删除ename
		ename.getParent().remove(ename);
		// 写回去
		try {
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("utf-8");

			XMLWriter xm = new XMLWriter(new FileOutputStream(XML_NAME), format);

			xm.write(doc);
			xm.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
